const User = require('../model/user');
const tools = require('../utils/tools');
let jwtUils = require('../utils/jwtUtils');




const getUserDataByName=async (username, resp)=>{
    let data = await User.getUserDataByName(username);
    resp.send({data})
}











let getUserlogin = async (req, resp) => {
  let query = req.body;
  let name = query.name;
  let pwd = query.pwd;
  let UserloginData = await User.getUser(name, pwd);
  if (UserloginData.length != 0) {
    //session存储,用于保持用户活性

    let jwt_token = jwtUtils.sign(
      { id: UserloginData[0].id },
      global.globalKey,
      3600,
    );
    //将登录token存入redis数据库缓存
    redisUtils.set('jwt_token', jwt_token, 3600);
    let u_id = UserloginData[0].id;
    resp.send({ UserloginData, jwt_token, u_id });
  } else {
    resp.status(500);
    resp.send('用户名或者账号输入错误');
  }
};

let jwtUtils = require('../utils/jwtUtils');
let crypto = require('crypto');
let resgintUtils = require('../utils/registUtil');
/*注册*/
let regist = async (req, resp) => {
  console.log('注册ing');
  let UserData = {};
  let body = req.body;
  let msg = null;
  UserData.username = body.username;
  //判断密码是否合理
  if (!resgintUtils.CheckPassWord(body.password)) {
    resp.status(500);
    resp.end('密码格式不正确');
  } else {
    console.log('else');
    //加密
    let MD5 = crypto.createHash('md5');
    let pwd_Md5 = MD5.update(body.password).digest('hex');
    console.log(pwd_Md5);
    UserData.password = pwd_Md5;
    if (!UserData.password || !UserData.username) {
      // resp.status()
      tools.error(resp, '参数丢失');
      return;
    }
    let tmp = await User.getUserNameTmp(UserData.username);
    console.log('用户信息');
    console.log(tmp);
    if (tmp.length != 0) {
      resp.status(500);
      resp.end('用户名已存在');
    } else {
      await User.setUser(UserData);
    }
    resp.send('注册成功');
  }
};

let bindUserEmils = async (req, resp) => {
  let token = req.query.token;
  let emildata = await jwtUtils.verifysync(token, global.globalKey);
  let falg = await User.getUserMailboxTmp(emildata.emil);
  if (falg.length != 0) {
    resp.send('该邮箱已经绑定过了');
  } else {
    resp.send('绑定成功');
    User.setEmil(emildata.emil, emildata.u_id, token);
  }
};

/**
 * 修改头像
 * @param req
 * @param resp
 * @returns {Promise<void>}
 */
let upUserHead = async (req, resp) => {
  User.upUserHead(req.head_imgUrl, req.u_id);
  resp.send();
};
/**
 * 修改名字
 * @param req
 * @param resp
 * @returns {Promise<void>}
 */
let upUserName = async (req, resp) => {
  User.upUserName(req.body.username, req.u_id);
  resp.send();
};

let getUserdata = async (req, resp) => {
  let redUserData = await redisUtils.get('UserloginData');
  redUserData = JSON.parse(redUserData);
  resp.send(redUserData);
};

let getUserName = async (req, resp) => {
  let u_id = req.body.u_id;
  let Username = await User.getUserNamebyId(u_id);
  Username = Username[0].username;
  resp.send(Username);
};
let getUserAll = async (req, resp) => {
  let u_id = req.body.u_id;
  let getUserAll = await User.getUserAllbyId(u_id);
  resp.send(getUserAll[0]);
};

let getUserNameAll = async (req, resp) => {
  let UsernameAll = await User.getUserNameAll();
  resp.send(UsernameAll);
};

module.exports = {
  getUserAll,
  getUserlogin,
  bindUserEmils,
  regist,
  upUserHead,
  upUserName,
  getUserdata,
  getUserName,
  getUserNameAll,
    getUserDataByName
};
